package cn.itsource.org.service.impl;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.basic.util.PageResult;
import cn.itsource.org.domain.TEmployee;
import cn.itsource.org.mapper.TEmployeeMapper;
import cn.itsource.org.query.TEmployeeQuery;
import cn.itsource.org.service.TEmployeeService;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
* @author 26871
* @description 针对表【t_employee】的数据库操作Service实现
* @createDate 2024-04-25 09:53:28
*/
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class TEmployeeServiceImpl implements TEmployeeService{
    @Autowired
    private TEmployeeMapper tEmployeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void insert(TEmployee tEmployee) {
        tEmployeeMapper.insert(tEmployee);

    }

    @Override
    @Transactional
    public void delete(Long id) {
        tEmployeeMapper.delete(id);
        logininfoMapper.delete(id);

    }

    @Override
    @Transactional
    public void update(TEmployee tEmployee) {
        tEmployeeMapper.update(tEmployee);

    }

    @Override
    public TEmployee selectById(Long id) {

        return tEmployeeMapper.selectById(id);
    }

    @Override
    public List<TEmployee> selectAll() {

        return tEmployeeMapper.selectAll();
    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        tEmployeeMapper.patchDelete(ids);
        logininfoMapper.patchDelete(ids);

    }


    @Override
    @Transactional
    public void save(TEmployee tEmployee) {
        //随机设置盐值
        String salt = RandomUtil.randomString(32);
        tEmployee.setSalt(salt);
        //盐值加密并且重新赋值密码
        String password = SecureUtil.md5(salt + tEmployee.getPassword());
        tEmployee.setPassword(password);
        if (tEmployee.getId() == null){
            tEmployeeMapper.insert(tEmployee);
            //构建logininfo对象
            Logininfo loginInfo = new Logininfo();
            BeanUtils.copyProperties(tEmployee,loginInfo);
            loginInfo.setType(0);
            logininfoMapper.insert(loginInfo);

        }else {
            tEmployeeMapper.update(tEmployee);
            Logininfo loginInfo = new Logininfo();
            BeanUtils.copyProperties(tEmployee,loginInfo);
            logininfoMapper.update(loginInfo);
        }

    }

    @Override
    public PageResult<TEmployee> selectByQueryPage(TEmployeeQuery tEmployeeQuery) {
        Integer total = tEmployeeMapper.selectCount(tEmployeeQuery);
        List<TEmployee> list = tEmployeeMapper.selectByQueryPage(tEmployeeQuery);
        System.out.println(list);
        return new PageResult<TEmployee>(total,list);
    }


}
